package Top4;

import com.rabbitmq.client.*;
import util.ConnectionUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: Administrator
 * @Date: 2019/3/3 23:21
 * @Description: 优先级队列  具有最高优先级的队列具有高的优先权
 */
public class MaxPriorityProducer {
    private static final String EXCHANGE_NAME = "transaction_exchange";
    private static final String ROUTING_KEY = "routingkey_demo";
    private static final String QUEUE_NAME = "transaction_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        String message = "Mandatory Test";
        //发送时候调用的是路由键
        // 创建一个 type="direct" 、持久化的、非自动删除的交换器
        channel.exchangeDeclare(EXCHANGE_NAME, "direct", true, false, null);

        HashMap<String, Object> map = new HashMap<String, Object>();
        // 设置最大优先级
        map.put("x-max-priority", 10);
        channel.basicPublish(EXCHANGE_NAME, "1111", true, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        //当交换器没有绑定任何队列
        channel.addReturnListener(new ReturnListener() {
            public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("=========handleReturn===method============");
                System.out.println("replyText:" + replyText);
                System.out.println("exchange:" + exchange);
                System.out.println("routingKey:" + routingKey);
                System.out.println("message:" + new String(body));
            }
        });

        connection.close();
    }
}
